import api from "../utils/request";

//用户登录
const Login = (data) => {
  return new Promise((resolve, reject) => {
    if (
      (data.LoginName === "Admin" && data.LoginPwd === "123456") ||
      (data.LoginName === "Login" && data.LoginPwd === "123456")
    ) {
      setTimeout(() => {
        return resolve({
          code: 0,
          msg: "登录成功",
          data: {
            name: data.LoginName,
            title: data.LoginName === "Admin" ? "超级管理员" : "管理员",
            ip: "云南",
            super: data.LoginName === "Admin",
            time: Date.now(),
          },
        });
      }, 1500);
    } else {
      setTimeout(() => {
        return reject({
          code: 1,
          msg: "账号或密码错误",
        });
      }, 1500);
    }
  });
};

//获取商品信息
function getGoodsList() {
  return api.get("/home/goodslist");
}

//获取商品数据数据
const getHomeList = () => {
  return api.get("/home/datalist");
};

/**
 * 分页获取用户信息列表
 * @param {Object} param
 * @param {Number} page 页码
 * @param {Number} limit 每页所需条数
 * @param {Number} count 从哪一条开始获取
 * @returns
 */
const getUserList = ({ page = 1, limit = 15, count = 0 }) => {
  return api.get("user/userlist", {
    params: {
      page,
      limit,
      count,
    },
  });
};

/**
 * 修改用户信息
 * @param {*} user 已经修改的用户信息
 * @returns
 */
const setUser = (user) => {
  return api.post("user/userlist", JSON.stringify(user));
};

/**
 * 删除指定用户
 * @param {*} id 用户id
 * @returns
 */
const deleteUser = (id) => {
  return api.delete("user/userlist", {
    params: {
      id,
    },
  });
};

/**
 * 搜索指定用户
 * @param {*} type 用户id或者用户姓名
 * @returns
 */
const getSearchUser = (type) => {
  return api.get("user/searchuser", {
    params: {
      type,
    },
  });
};

/**
 * 添加用户
 * @param {Object} from 用户信息表单
 * @returns
 */
const addUser = (from) => {
  const data = JSON.stringify(from);
  return api.post("user/adduser", data);
};

//获取mall页面商品数据
const getMallData = () => {
  return api.get("mall/mallList");
};

//修改商品上下架
const setMallOff = (obj) => {
  return api.post("mall/setoff", JSON.stringify(obj));
};

//修改商品信息
const setMallData = (form) => {
  return api.put("mall/update", JSON.stringify(form));
};

//添加商品
const addMallData = (from) => {
  return api.post("mall/post", JSON.stringify(from));
};

export default {
  Login,
  getGoodsList,
  getHomeList,
  getUserList,
  setUser,
  deleteUser,
  getSearchUser,
  addUser,
  getMallData,
  setMallOff,
  setMallData,
  addMallData,
};
